package com.lapelittle.framework.service;

import com.lapelittle.framework.entity.SysUser;
import com.lapelittle.framework.entity.SysUserToken;
import com.lapelittle.framework.mapper.SysUserMapper;
import com.lapelittle.framework.model.LoginRequest;
import com.lapelittle.framework.model.LoginVO;
import com.lapelittle.framework.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class LoginService {
    @Autowired
    private SysUserMapper userMapper;
    
    @Autowired
    private EmailService emailService;
    
    @Autowired
    private TokenService tokenService;
    
    public LoginVO login(LoginRequest request) {
        if (request.getLoginType() == null) {
            throw new BusinessException("登录类型不能为空");
        }
        
        if (request.getUsername() == null || request.getUsername().trim().isEmpty()) {
            throw new BusinessException("用户名不能为空");
        }
        
        SysUser user = null;
        String password = request.getPassword();
        
        switch (request.getLoginType()) {
            case 1: // 用户名密码登录
                user = userMapper.findByUsername(request.getUsername());
                user = checkPassword(user, password);
                break;
                
            case 2: // 手机号密码登录
                user = userMapper.findByMobile(request.getUsername());
                user = checkPassword(user, password);
                break;
                
            case 3: // 邮箱密码登录
                user = userMapper.findByEmail(request.getUsername());
                user = checkPassword(user, password);
                break;
                
            case 4: // 邮箱验证码登录
                user = userMapper.findByEmail(request.getUsername());
                if (user != null) {
                    if (emailService.verifyCode(request.getUsername(), request.getCode())) {
                        break;
                    }
                }
                user = null;
                break;
                
            default:
                throw new RuntimeException("不支持的登录方式");
        }
        
        if (user != null) {
            LoginVO loginVO = new LoginVO();
            user.setPassword(null); // 清除密码
            // 确保所有字段都被返回
            SysUser fullUser = userMapper.findById(user.getId());
            if (fullUser != null) {
                fullUser.setPassword(null);
                loginVO.setUser(fullUser);
            } else {
                loginVO.setUser(user);
            }
            // 生成token
            SysUserToken token = tokenService.createToken(user.getId());
            loginVO.setToken(token.getToken());
            return loginVO;
        }
        
        return null;
    }
    
    private SysUser checkPassword(SysUser user, String password) {
        if (user != null) {
            // 将密码进行MD5加密后比较
            String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
            if (user.getPassword().equals(md5Password)) {
                return user;
            }
        }
        return null;
    }
} 